<template>
  <div class="app-container">
    <el-card>
      <el-form ref="searchForm" :model="searchForm" size="mini" :inline="true">
        <div>
          <el-form-item label="姓名" prop="realname">
            <el-input
              style="width: 200px"
              maxlength="200"
              clearable
              v-model="searchForm.realname"
              placeholder="请输入企业名称"
            />
          </el-form-item>

          <el-form-item label="联系方式">
            <el-input
              style="width: 200px"
              v-model="searchForm.phone"
              maxlength="11"
              clearable
              placeholder="请输入联系方式"
            />
          </el-form-item>

          <el-form-item label="所属经理">
            <el-select
              style="width: 200px"
              v-model="searchForm.adminId"
              placeholder="请选择所属经理"
              filterable
              clearable
            >
              <el-option
                v-for="item in managerList"
                :key="item.id"
                :label="item.name + ' ' + '(' + item.phone + ')'"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="业务类型" style="display: inline-block">
            <el-select
              style="width: 200px"
              v-model="searchForm.businessType"
              placeholder="请选择业务类型"
            >
              <el-option
                v-for="item in businessTypeList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </div>
        <div>
          <el-form-item label="添加时间">
            <el-date-picker
              style="width: 200px"
              v-model="searchForm.beginTime"
              align="right"
              type="date"
              placeholder="选择开始日期"
              :picker-options="pickerOptions"
              value-format="yyyy-MM-dd"
              format="yyyy 年 MM 月 dd 日"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item>-</el-form-item>
          <el-form-item>
            <el-date-picker
              v-model="searchForm.endTime"
              align="right"
              type="date"
              placeholder="选择结束日期"
              :picker-options="pickerOptions"
              value-format="yyyy-MM-dd"
              format="yyyy 年 MM 月 dd 日"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-refresh" @click="resetList"
              >重置</el-button
            >
            <el-button
              icon="el-icon-search"
              type="primary"
              @click="getlist('searchForm')"
              >查询</el-button
            >
          </el-form-item>
        </div>
      </el-form>
      <div style="display: flex">
        <router-link
          v-btnpermission="$btnPer.customerPersonBtnrules.addCustomer"
          :to="{ path: '/createdPersonCustomer' }"
        >
          <el-button size="mini" type="primary" icon="el-icon-plus"
            >新建个人客户</el-button
          >
        </router-link>
        <el-button
          v-btnpermission="$btnPer.customerPersonBtnrules.batchDelete"
          style="margin-left: 10px"
          size="mini"
          icon="el-icon-delete"
          @click="deleteMany()"
          >批量删除</el-button
        >
      </div>
      <div style="margin-top: 20px">
        <el-table
          :data="tableData"
          size="mini"
          @selection-change="selectTableList"
          :default-sort="{ prop: 'date', order: 'descending' }"
          v-loading="listLoading"
          border
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column
            label="编号"
            width="100"
            align="center"
            type="index"
          ></el-table-column>
          <el-table-column
            align="center"
            label="姓名"
            prop="realname"
          ></el-table-column>
          <el-table-column
            sortable
            align="center"
            label="来访日期"
            prop="visitTime"
          ></el-table-column>
          <el-table-column
            align="center"
            label="联系方式"
            prop="phone"
          ></el-table-column>
          <el-table-column align="center" label="业务类型" prop="businessType">
            <template slot-scope="scope">
              {{ scope.row.businessType | businessTypeFilter }} </template
            >>
          </el-table-column>
          <el-table-column
            align="center"
            label="客户经理"
            prop="adminNickName"
          ></el-table-column>
          <el-table-column
            align="center"
            label="添加时间"
            prop="createTime"
            sortable
          ></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="text" @click="detail(scope.row)" size="small"
                >详情</el-button
              >
              <router-link
                v-btnpermission="$btnPer.customerPersonBtnrules.editCustomer"
                :to="{path: '/createdPersonCustomer', query:{id: scope.row.id}}, "
              >
                <el-button type="text" size="small">修改</el-button>
              </router-link>
              <el-button
                v-btnpermission="$btnPer.customerPersonBtnrules.deleteCustomer"
                size="small"
                @click="deleteItem(scope.row)"
                type="text"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        class="pageRight"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 20, 30, 40]"
        :current-page="searchForm.pageNum"
        :page-size="searchForm.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <enterpriseDetail ref="detailRef"></enterpriseDetail>
  </div>
</template>

<script>
import enterpriseDetail from "../components/personDetail.vue";
import { getCustomerPersonList, deleteCustomerPerson } from "@/api/customer";
import { timeFun } from "../../../utils/date";
import { datePickerOptions } from "../options";
import { getUserList } from "@/api/dept";
import { re } from "semver";
export default {
  components: {
    enterpriseDetail
  },
  filters: {
    businessTypeFilter(val) {
      const mapObj = {
        1: "贷款业务",
        2: "孵化业务"
      };
      return mapObj[val];
    }
  },
  data() {
    return {
      searchForm: {
        realname: "",
        phone: "",
        businessType: "",
        adminId: "",
        beginTime: "",
        endTime: "",
        pageNum: 1,
        pageSize: 10
      },
      managerList: [],
      provinceList: [],
      businessTypeList: [
        {
          name: "贷款业务",
          id: 1
        },
        {
          name: "孵化业务",
          id: 2
        }
      ],
      pickerOptions: datePickerOptions,
      listLoading: false,
      tableData: [],
      total: 0,
      deleteList: []
    };
  },
  created() {
    this.getUserList();
    this.getlist();
  },
  methods: {
    getUserList() {
      getUserList().then(res => {
        this.managerList = res.data.map(v => {
          return {
            name: v.nickName,
            id: v.adminId,
            phone: v.phoneNumber
          };
        });
      });
    },
    selectTableList(val) {
      // console.log(val);
      this.deleteList = val.map(v => v.id);
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val;
      this.getlist();
    },
    handleCurrentChange(val) {
      this.searchForm.pageNum = val;
      this.getlist();
    },
    deleteItem(val) {
      let that = this;
      this.$confirm("是否确认删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        deleteCustomerPerson(val.id).then(res => {
          if (res.code == 200) {
            that.$message({
              type: "success",
              message: "删除成功!"
            });
            setTimeout(() => {
              that.getlist();
            }, 500);
          }
        });
      });
    },
    detail(row) {
      this.$refs.detailRef.opendialog(row);
    },
    deleteMany() {
      let that = this;
      if (this.deleteList.length == 0) {
        this.$message({
          message: "请先选择要删除的项",
          type: "warning"
        });
      } else {
        this.$confirm("是否确认删除?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          deleteCustomerPerson(this.deleteList.join(",")).then(res => {
            if (res.code == 200) {
              that.$message({
                type: "success",
                message: "删除成功!"
              });
              setTimeout(() => {
                that.getlist();
              }, 500);
            }
          });
        });
      }
    },
    getlist() {
      if (
        (this.searchForm.beginTime && !this.searchForm.endTime) ||
        (!this.searchForm.beginTime && this.searchForm.endTime)
      ) {
        return this.$message.warning("请填写开始日期或者结束日期哦");
      }
      if (this.searchForm.beginTime && this.searchForm.endTime) {
        if (!timeFun(this.searchForm.beginTime, this.searchForm.endTime)) {
          return this.$message.warning("结束时间必须大于开始时间");
        }
      }
      this.listLoading = true;
      getCustomerPersonList(this.searchForm)
        .then(res => {
          this.tableData = res.data.list;
          this.total = res.data.total;
          this.listLoading = false;
        })
        .finally(() => {
          this.listLoading = false;
        });
    },
    resetList() {
      this.searchForm = {
        realname: "",
        phone: "",
        businessType: "",
        adminId: "",
        beginTime: "",
        endTime: "",
        pageNum: 1,
        pageSize: 10
      };
      this.getlist();
    }
  }
};
</script>

<style lang="scss" scoped>
.app-container {
  height: calc(100vh - 50px);
  background-color: #f5f5f5;
  position: relative;
}
.el-form-item--mini.el-form-item,
.el-form-item--small.el-form-item {
}
</style>
